// Copyright (c) 2011 Vratislav Cermak (vratacermak@gmail.com)
//  
// This file is part of SrtSync Project. See http://code.google.com/p/srtsync/ for more information.
//  
// SrtSync is an open source project. Using of SrtSync is free of charge.
// It is distributed under GNU General Public License version 3 (GPLv3).
// GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
//  
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
using System;
using System.IO;
using System.Text;
using SrtSync.Core;
using SrtSync.Core.Framework;

namespace SrtSync.GtkProgram
{
    public class Dialogs : IDialogs
    {
        private Gtk.Window _parent;
        private ISettings _settings;
        
        public Dialogs(Gtk.Window parent, ISettings settings)
        {
            _parent = parent;
            _settings = settings;
        }

        public bool ShowQuestion(string question)
        {
            using (Gtk.MessageDialog dialog = new Gtk.MessageDialog(_parent, Gtk.DialogFlags.Modal, Gtk.MessageType.Question, Gtk.ButtonsType.YesNo, question, new object()))
            {
                dialog.WindowPosition = Gtk.WindowPosition.Center;
                dialog.Title = "SrtSync";
                dialog.DefaultResponse = Gtk.ResponseType.Yes;
                bool result = dialog.Run() == (int)Gtk.ResponseType.Yes;
                dialog.Destroy();
                return result;
            }
        }

        public void ShowError(string error)
        {
            ShowError(error, null);
        }

        public void ShowError(string error, Exception exception)
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendLine(error);
            if (exception != null)
            {
                builder.AppendLine();
                builder.Append(String.Format("Error: {0}", exception.Message));
            }
            
            // dialog itself calls String.Format on input string, bypass it
            string message = builder.ToString().Replace("{", "(").Replace("}", ")");
                
            using (Gtk.MessageDialog dialog = new Gtk.MessageDialog(_parent, Gtk.DialogFlags.Modal, Gtk.MessageType.Error, Gtk.ButtonsType.Ok, message, new object()))
            {
                dialog.WindowPosition = Gtk.WindowPosition.Center;
                dialog.Title = "SrtSync";
                dialog.DefaultResponse = Gtk.ResponseType.Ok;
                dialog.Run();
                dialog.Destroy();
            }
        }

        public void ShowInfo(string message)
        {
            using (Gtk.MessageDialog dialog = new Gtk.MessageDialog(_parent, Gtk.DialogFlags.Modal, Gtk.MessageType.Info, Gtk.ButtonsType.Ok, message, new object()))
            {
                dialog.WindowPosition = Gtk.WindowPosition.Center;
                dialog.Title = "SrtSync";
                dialog.DefaultResponse = Gtk.ResponseType.Ok;
                dialog.Run();
                dialog.Destroy();
            }
        }

        public bool OpenFile(out string path)
        {
            path = null;
            using (Gtk.FileChooserDialog dialog = new Gtk.FileChooserDialog("Open File", _parent, Gtk.FileChooserAction.Open, new object[] { }))
            {
                if (Directory.Exists(_settings.Folder))
                {
                    dialog.SetCurrentFolder(_settings.Folder);
                }
                dialog.WindowPosition = Gtk.WindowPosition.Center;
                dialog.AddButton(Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
                dialog.AddButton(Gtk.Stock.Ok, Gtk.ResponseType.Ok);
                dialog.DefaultResponse = Gtk.ResponseType.Ok;
                dialog.SelectMultiple = false;

                Gtk.FileFilter srtFilter = new Gtk.FileFilter();
                srtFilter.Name = "Srt Files (*.srt)";
                srtFilter.AddPattern("*.srt");
                dialog.AddFilter(srtFilter);
                Gtk.FileFilter defaultFilter = new Gtk.FileFilter();
                defaultFilter.Name = "All Files (*.*)";
                defaultFilter.AddPattern("*.*");
                dialog.AddFilter(defaultFilter);

                if (dialog.Run() == (int)Gtk.ResponseType.Ok)
                {
                    path = dialog.Filename;                    
                    _settings.Folder = Path.GetDirectoryName(path);
                }
                dialog.Destroy();
            }

            return !String.IsNullOrEmpty(path) && File.Exists(path);
        }

        public bool SaveFile(out string path)
        {
            path = null;
            bool cancel = false;
            using (Gtk.FileChooserDialog dialog = new Gtk.FileChooserDialog("Save As", _parent, Gtk.FileChooserAction.Save, new object[] { }))
            {
                if (Directory.Exists(_settings.Folder))
                {
                    dialog.SetCurrentFolder(_settings.Folder);
                }
                dialog.WindowPosition = Gtk.WindowPosition.Center;
                dialog.AddButton(Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
                dialog.AddButton(Gtk.Stock.Ok, Gtk.ResponseType.Ok);
                dialog.DefaultResponse = Gtk.ResponseType.Ok;
                dialog.SelectMultiple = false;

                dialog.FileActivated += delegate
                {
                    if (File.Exists(dialog.Filename))
                    {
                        if (!ShowQuestion(String.Format("File '{0}' already exists. Are you sure?", dialog.Filename)))
                        {
                            cancel = true;
                        }
                    }
                };

                Gtk.FileFilter srtFilter = new Gtk.FileFilter();
                srtFilter.Name = "Srt Files (*.srt)";
                srtFilter.AddPattern("*.srt");
                dialog.AddFilter(srtFilter);
                Gtk.FileFilter defaultFilter = new Gtk.FileFilter();
                defaultFilter.Name = "All Files (*.*)";
                defaultFilter.AddPattern("*.*");
                dialog.AddFilter(defaultFilter);

                if (dialog.Run() == (int)Gtk.ResponseType.Ok)
                {
                    path = dialog.Filename;
                    _settings.Folder = Path.GetDirectoryName(path);
                }
                dialog.Destroy();
            }

            return !String.IsNullOrEmpty(path) && !cancel;
        }

        public void ShowAboutDialog()
        {
            using (Gtk.AboutDialog dialog = new Gtk.AboutDialog())
            {
                dialog.TransientFor = _parent;
                dialog.WindowPosition = Gtk.WindowPosition.Center;
                dialog.ProgramName = About.ProgramName;
                dialog.Version = About.Version;
                dialog.Copyright = About.Copyright;
                dialog.License = About.License;
                dialog.WrapLicense = true;
                dialog.Authors = About.Authors;
                dialog.Website = About.Website;

                dialog.Run();
                dialog.Destroy();
            }
        }
        
        public bool EditText(ref string text)
        {
            bool result = false;
            using (EntryEditorDialog dialog = new EntryEditorDialog(_parent))
            {
                dialog.Text = text;
                if (dialog.Run() == (int)Gtk.ResponseType.Ok)
                {
                    text = dialog.Text;
                    result = true;
                }
                dialog.Destroy();
            }
            return result;
        }
    }
}